<?php 
Class formsController extends appController
{
	/**
	 * 显示表单列表
	 */
	public function index()
	{
		$page = yf_empty(intval($_GET['page']), 1, true);
		$art_list = Model('forms')->forms_list($page);
		$this->assign('art_list', $art_list['data']);
		$this->assign('art_page', $art_list['pagelink']);
		$this->display('index.html');
	}
	
	/**
	 * 显示表单数据
	 */
	public function log()
	{
		$arr = array("error" => 0, 'content' => '');
		if($_POST)
		{
			$this->admin_priv('entry_log', 2);
			
			$arr = array("error" => 1, 'content' => '');
			$form_id = intval($_POST['form_id']);
			$file = $_FILES['file'];
			if(empty($form_id))
			{
				$arr['content'] = "系统错误";
			}
			else 
			{
				if($file['error'] == 0)
				{
					$table = Model('forms')->get_table_type($form_id);
					if(!empty($table))
					{
						$check = yf_entry($table, 'forms_log_'.$form_id, $file);
						if($check)
						{
							$arr['content'] = "导入成功";
						}
						else 
						{
							$arr['content'] = "导入失败";
						}
					}
					else 
					{
						$arr['content'] = "表单字段为空,上传失败";
					}
				}
				else
				{
					$arr['content'] = "请选择上传的文件";
				}
			}
		}
		
		$page = yf_empty(intval($_REQUEST['page']), 1, true);
		$id = intval($_GET[id]);
		$table_list = Model('forms')->get_table_list($id);
		$art_list = Model('forms')->log_list($id, $table_list['val'], $page);
		$this->assign('art_list',$art_list['data']);
		$this->assign('art_page',$art_list['pagelink']);
		$this->assign("table_list", $table_list);
		$this->assign("form_id", $id);
		$this->assign("arr", $arr);
		$this->display('log.html');
	}
	
	/**
	 * 删除表单数据
	 */
	public function del_log()
	{
		$this->admin_priv('del_log');
		$res = array('error' => 1, 'content' => '删除失败');
		$form_id = yf_empty(intval($_GET['form_id']), "系统错误");
		$id = yf_empty(intval($_GET['id']), "系统错误");
		$check = Model('forms')->del_log($form_id, $id);
		if($check)
		{
			$res = array('error' => 0, 'content' => '删除成功');
		}
		$this->showMsg($res);
	}
	
	/**
	 * 导出表单数据
	 */
	public function dao_log()
	{
		$this->admin_priv('dao_log', 2);
		$id = yf_empty(intval($_GET['id']), 'System Error');
		$table_list = Model('forms')->get_table_list($id, 1);
		$info = Model('forms')->get_all_info_log($id, $table_list['val']);
		$name = Model('forms')->get_form_name($id);
		yf_daochu($table_list['table'], $info, $name);
		echo "Success";
	}
	
	/**
	 * 显示新建表单页面
	 */
	public function add_form()
	{
		$this->admin_priv('add_form', 2);
		$this->assign('action', 'new_form');
		$this->display('forms.html');
	}
	
	/**
	 * 添加新的表单
	 */
	public function new_form()
	{
		$this->admin_priv('add_form');
		$result = array('error' => 1, 'content' => '');
		$info['name'] = yf_empty($_POST['title'], "表单标题不能为空");
		$info['submit_interval'] = yf_empty($_POST['interval'], 0, true);
		$info['stime'] = yf_empty($_POST['stime'], 0, true) == 0 ? 0 : strtotime($_POST['stime']);
		$info['etime'] = yf_empty($_POST['etime'], 0, true) == 0 ? 0 : strtotime($_POST['etime']);
		$info['created'] = yf_time();
		
		$is_exist = Model('forms')->check_form_name($info['name']);
		
		if($is_exist > 0)
		{
			$result['content'] = '该表单名已经存在,请换一个表单名，这样才好区分';
			$this->showMsg($result);	
		}
		
		$id = Model('forms')->insert_forms($info);
		if($id)
		{
			$result['error'] = 0;
			$result['content'] = "建表成功";
		}
		else 
		{
			$result['content'] = Lang('INVALID_OPERATION');
		}
 		$this->showMsg($result);
	}
	
	/**
	 * 显示编辑表单配置信息页面
	 */
	public function edit_form()
	{
		$this->admin_priv('edit_form', 2);
		$id = intval($_GET['id']);
		$info = Model('forms')->get_form($id);
		$this->assign('id', $id);
		$this->assign('art', $info);
		$this->assign('action', 'update_form');
		$this->display('forms.html');
	}
	
	/**
	 * 更新表单配置信息
	 */
	public function update_form()
	{
		$this->admin_priv('edit_form');
		$result = array('error' => 1, 'content' => '');
		$info['name'] = yf_empty($_POST['title'], "表单标题不能为空");
		$info['submit_interval'] = yf_empty($_POST['interval'], 0, true);
		$info['stime'] = yf_empty($_POST['stime'], 0, true) == 0 ? 0 : strtotime($_POST['stime']);
		$info['etime'] = yf_empty($_POST['etime'], 0, true) == 0 ? 0 : strtotime($_POST['etime']);
		
		$id = yf_empty($_POST['id'], '', true);
		
		$is_exist = Model('forms')->check_form_name($info['name'], $id);
		
		if($is_exist > 0)
		{
			$result['content'] = '该表单名已经存在,请换一个表单名，这样才好区分';
			$this->showMsg($result);	
		}
		
		$data = Model('forms')->update_forms($info, $id);
		
		if($data)
		{
			$result['error'] = 0;
			$result['content'] = "编辑成功";
		}
		else 
		{
			$result['content'] = '编辑失败';
		}
		$this->showMsg($result);
	}
	
	/**
	 * 显示字段列表
	 */
	public function field_list()
	{
		$page = yf_empty(intval($_REQUEST['page']), 1, true);
		$art_list = Model('forms')->get_field_list($page);
		$this->assign('art_list', $art_list['result']);
		$this->assign('art_page', $art_list['page']);
		$this->display('field_list.html');
	}
	
	/**
	 * 显示字段编辑页面
	 */
	public function edit_field()
	{
		$this->admin_priv('edit_word', 2);
		$id = yf_empty(intval($_GET[id]), '', true);
		$art = Model('forms')->get_field($id);
		$this->assign('art', $art);
		$this->assign('id', $id);
		$this->assign('action', 'update_field');
		$this->display('field.html');
	}
	
	/**
	 * 更新字段信息
	 */
	public function update_field()
	{
		$this->admin_priv('edit_word');
		$result = array('error' => 1, 'content' => '');
		$info['data'] = yf_empty($_POST['data'], '数据库字段不能为空');
		$info['type'] = yf_empty($_POST['type'], 0, true);
		$en_name = yf_empty($_POST['en_name'], '', true);
		$name = yf_empty($_POST['name'], '注释名称不能为空');
		$info['name'] = isset($_POST['name']) ? CH_SC.$name.DIG_EC.EN_SC.$en_name.DIG_EC : '';
		$en_tips = yf_empty($_POST['en_tips'], '', true);
		$info['tips'] = isset($_POST['tips']) ? CH_SC.$_POST['tips'].DIG_EC.EN_SC.$en_tips.DIG_EC : '';
		$en_default = yf_empty($_POST['en_default'], '', true);
		$info['default_val'] = isset($_POST['default']) ? CH_SC.$_POST['default'].DIG_EC.EN_SC.$en_default.DIG_EC : '';
		$en_field = is_array($_POST['en_field']) ? implode('::',$_POST['en_field']) : $_POST['en_field'];
		$info['field'] = is_array($_POST['field']) ? CH_SC.implode('::',$_POST['field']).DIG_EC.EN_SC.$en_field.DIG_EC : CH_SC.$_POST['field'].DIG_EC.EN_SC.$en_field.DIG_EC;
		$info['pattern'] = yf_empty($_POST['pattern'], 0, true);
		$id = yf_empty(intval($_POST['id']), '', true);
		if(hy_check($info['data'], 'un') == '#@_error')
		{
			$result['content'] = '数据库字段不合规范';
			$this->showMsg($result);
		}
		
		$check_id = Model('forms')->check_same_data($info['data'], $id);
		if($check_id > 0)
		{
			$result['content'] = "该数据库字段已存在，请换一个";
			$this->showMsg($result);
		}
		
		$res = Model('forms')->update($info, array('table' => 'fields', 'id' => $id));
		if($res)
		{
			$result['error'] = 0;
			$result['content'] = Lang('SUCCESS_EDIT');
		}
		else
		{
			$result['content'] = Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	}
	
	/**
	 * 添加字段信息
	 */
	public function new_field()
	{
		$this->admin_priv('add_word');
		$result = array('error' => 1, 'content' => '');
		$info['data'] = yf_empty($_POST['data'], '数据库字段不能为空');
		$info['type'] = yf_empty($_POST['type'], 0, true);
		$en_name = yf_empty($_POST['en_name'], '', true);
		$name = yf_empty($_POST['name'], '注释名称不能为空');
		$info['name'] = isset($_POST['name']) ? CH_SC.$name.DIG_EC.EN_SC.$en_name.DIG_EC : '';
		$en_tips = yf_empty($_POST['en_tips'], '', true);
		$info['tips'] = isset($_POST['tips']) ? CH_SC.$_POST['tips'].DIG_EC.EN_SC.$en_tips.DIG_EC : '';
		$en_default = yf_empty($_POST['en_default'], '', true);
		$info['default_val'] = isset($_POST['default']) ? CH_SC.$_POST['default'].DIG_EC.EN_SC.$en_default.DIG_EC : '';
		$en_field = is_array($_POST['en_field']) ? implode('::',$_POST['en_field']) : $_POST['en_field'];
		$info['field'] = is_array($_POST['field']) ? CH_SC.implode('::',$_POST['field']).DIG_EC.EN_SC.$en_field.DIG_EC : CH_SC.$_POST['field'].DIG_EC.EN_SC.$en_field.DIG_EC;
		$info['pattern' ] =  yf_empty($_POST['pattern'], 0, true);
		
		if(hy_check($info['data'], 'un') == '#@_error')
		{
			$result['content'] = '数据库字段不合规范';
			$this->showMsg($result);
		}
		
		$id = Model('forms')->check_same_data($info['data']);
		
		if($id > 0)
		{
			$result['content'] = "该数据库字段已存在，请换一个";
			$this->showMsg($result);
		}
		
		$res = Model('forms')->insert($info,'fields');
		if($res)
		{
			$result['error'] = 0;
			$result['content'] = Lang('SUCCESS_ADD');
		}
		else 
		{
			$result['content'] = Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	}
	
	/**
	 * 显示新建字段页面
	 */
	public function add_field()
	{
		$this->admin_priv('add_word', 2);
		$this->assign('action','new_field');
		$this->display('field.html');
	}
	
	/**
	 * 删除字段
	 */
	public function delfield()
	{
		$this->admin_priv('del_word');
		$res = array('error' => 1, 'content' => '删除失败');
		$id = yf_empty($_REQUEST['id'], '', true);
		if(is_array($id))
		{
			foreach($id as $k => $v)
			{
				$result = Model('forms')->del_field($v);
			}
		}
		else
		{
			$result = Model('forms')->del_field($id);
		}
		$this->showMsg($res);
	}
	
	/**
	 * 管理表字段
	 */
	public function manage_word()
	{
		$page = yf_empty(intval($_REQUEST['page']), 1, true);
		$id = yf_empty($_GET['id'], '', true);
		$list = Model('forms')->get_form_info($id, $page);
		$this->assign("list", $list['result']);
		$this->assign("page", $list['page']);
		$this->assign("id", $id);
		$this->display('word_list.html');
	}
	
	/**
	 * 显示添加表单字段页面
	 */
	public function add_word()
	{
		$this->admin_priv('add_form_word', 2);
		$form_id = yf_empty(intval($_GET['id']), '', true);
		$field_list = Model('forms')->get_all_field();
		$this->assign("field", $field_list);
		$this->assign("form_id", $form_id);
		$this->assign("action", "insert_word");
		$this->display('word.html');
	}
	
	/**
	 * 添加表字段
	 */
	public function insert_word()
	{
		$this->admin_priv('add_form_word');
		$res = array('error' => 1, 'content' => '');
		$info['form_id'] = yf_empty(intval($_POST['form_id']), "系统错误");
		$info['fields_id'] = yf_empty(intval($_POST['fields_id']), 0, true);
		
		if($info['fields_id'] == 0)
		{
			$res['content'] = "请选择字段";
			$this->showMsg($res);
		}
		
		$info['type'] = yf_empty($_POST['type'], 0, true);
		
		if($info['type'] == '0')
		{
			$res['content'] = "请选择字段类型";
			$this->showMsg($res);
		}
		
		$info['type_size'] = yf_empty(intval($_POST['type_size']), "字段大小不能为空");
		$info['status'] = yf_empty(intval($_POST['status']), 0, true);
		$info['is_list'] = yf_empty(intval($_POST['is_list']), 0, true);
		
		$check_exist = Model('forms')->check_fields_exist($info['form_id'], $info['fields_id']);
		if($check_exist > 0)
		{
			$res['content'] = "该表中已经存在该字段";
			$this->showMsg($res);
		}
		$id = Model('forms')->insert_table($info);
		
		if($id)
		{
			$res['error'] = 0;
			$res['content'] = "添加成功";
		}
		else
		{
			$res['content'] = "添加失败";
		}
		$this->showMsg($res);
	}
	
	/**
	 * 表单字段显示排列
	 */
	public function ordering()
	{
		$this->admin_priv('order_form_word');
		$res = array('error' => 1, 'content' => '');
		$id = yf_empty(intval($_POST['id']), '');
		$val = yf_empty(intval($_POST['val']), '');
		$check = Model('forms')->update_ordering($id, $val);
		if($check)
		{
			$res = array('error' => 0, 'content' => '');
		}
		$this->showMsg($res);
	}
	
	/**
	 * 显示表单字段编辑页面
	 */
	public function edit_word()
	{
		$this->admin_priv('edit_form_word', 2);
		$id = yf_empty(intval($_GET['id']), '', true);
		$info = Model('forms')->get_one_field($id);
		$field_list = Model('forms')->get_all_field();
		$this->assign("field", $field_list);
		$this->assign("id", $id);
		$this->assign("info", $info);
		$this->assign("form_id", $info['form_id']);
		$this->assign("action", "update_word");
		$this->display('word.html');
	}
	
	/**
	 * 更新表单字段数据
	 */
	public function update_word()
	{
		$this->admin_priv('edit_form_word');
		$res = array('error' => 1, 'content' => '');
		$id = yf_empty(intval($_POST['id']), "系统错误");
		$info['type'] = yf_empty($_POST['type'], 0, '');
		
		if($info['type'] == '0')
		{
			$res['content'] = "请选择字段类型";
			$this->showMsg($res);
		}
		
		$info['type_size'] = yf_empty(intval($_POST['type_size']), "字段大小不能为空");
		$info['status'] = yf_empty($_POST['status'], 0, true);
		$info['is_list'] = yf_empty($_POST['is_list'], 0, true);
		
		$check = Model('forms')->update_word($info, $id);
		if($check)
		{
			$res = array('error' => 0, 'content' => '编辑成功');
		}
		else
		{
			$res['content'] = "编辑失败";
		}
		$this->showMsg($res);
	}
	
	/**
	 * 删除表单的字段信息
	 */
	public function del_word()
	{
		$this->admin_priv('del_form_word');
		$res = array('error' => 1, 'content' => '删除失败');
		$id = yf_empty(intval($_REQUEST['id']), '系统错误');
		$check = Model('forms')->del_word($id);
		if($check)
		{
			$res = array('error' => 0, 'content' => '删除成功');
		}
		$this->showMsg($res);
	}
	
	/**
	 * 删除表单
	 */
	public function del_form()
	{
		$this->admin_priv('del_form');
		$res = array('error' => 1, 'content' => '删除失败');
		$id = yf_empty(intval($_REQUEST['id']), '系统错误');
		$check = Model('forms')->del_form($id);
		if($check)
		{
			$res = array('error' => 0, 'content' => '删除成功');
		}
		$this->showMsg($res);
	}
	
}